package management

import (
	"context"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type MongodbMgr struct {
	client        *mongo.Client
	MongdbConnect *mongo.Database
}

var (
	G_MongoMgr *MongodbMgr
)

func InitMongodbMgr() (err error) {
	var (
		client *mongo.Client
	)
	client, err = mongo.NewClient(options.Client().ApplyURI(G_config.MongodbUri).SetMaxPoolSize(8))
	if err != nil {
		err = err
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Duration(G_config.MongodbConnectTimeout)*time.Millisecond)
	defer cancel()
	err = client.Connect(ctx)
	if err != nil {
		err = err
	}

	G_MongoMgr = &MongodbMgr{
		client:        client,
		MongdbConnect: client.Database(G_config.AppDBName),
	}

	// fmt.Println("init userMgr")

	return
}
